//
//  MADareInfoViewController.m
//  MateAdv
//
//  Created by 茶 on 2023/9/13.
//

#import "MADareInfoViewController.h"
#import "MAButton.h"
#import "MADareViewController.h"

#define DARE_F @"Dare_f"
#define DARE_M @"Dare_m"

@interface MADareInfoViewController ()

@property (nonatomic, strong) NSArray *btns;

@property (nonatomic, strong) UIView *fNameView;
@property (nonatomic, strong) UIImageView *fImgView;
@property (nonatomic, strong) UITextField *fTextField;

@property (nonatomic, strong) UIView *mNameView;
@property (nonatomic, strong) UIImageView *mImgView;
@property (nonatomic, strong) UITextField *mTextField;

@property (nonatomic, strong) UIImageView *arrowView;

@property (nonatomic, assign) NSInteger type;

@end

@implementation MADareInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _type = 1;
    [self initInterface];
}

- (void)initInterface {
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
//    self.navigationController.navigationBar.backIndicatorImage = [[UIImage imageNamed:@"nav_back_btn"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationController.navigationBar.backIndicatorTransitionMaskImage = [[UIImage imageNamed:@"nav_back_btn"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    self.navigationItem.backBarButtonItem = backItem;
//    
    self.view.backgroundColor = MAColor(@"#FF0071");
    
    UIImageView *titleView = [UIImageView initWithName:@"Dare_Title"];
    [self.view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.ma_offset(20);
        make.top.mas_equalTo(self.view.mas_safeAreaLayoutGuideTop);
    }];
    
    UIView *contentView = [UIView new];
    contentView.backgroundColor = UIColor.whiteColor;
    contentView.layer.cornerRadius = MARatioWidth(20);
    contentView.clipsToBounds = YES;
    [self.view addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleView.mas_bottom).ma_offset(30);
        make.left.ma_offset(16);
        make.right.ma_offset(-16);
        make.height.ma_offset(409);
    }];
    
    NSArray *titles = @[@"男男", @"男女", @"女女"];
    NSArray *imgNames = @[@"Dare_mm", @"Dare_mf", @"Dare_ff"];
    
    NSMutableArray *btns = [NSMutableArray arrayWithCapacity:titles.count];
    for (int i = 0; i < titles.count; i++) {
        MAButton *button = [MAButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, MARatioWidth(40), MARatioHeight(60));
        button.tag = i;
        [button setTitleColor:MAColor(@"#999999") forState:UIControlStateNormal];
        [button setTitleColor:MAColor(@"#FF0071") forState:UIControlStateSelected];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button.titleLabel setFont:MAFontScaleRegular(14)];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_0", imgNames[i]]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_1", imgNames[i]]] forState:UIControlStateSelected];
        [button setButtonImageTitleStyle:ButtonImageTitleStyleTop padding:4];
        [button addTarget:self action:@selector(clickEventWithButton:) forControlEvents:UIControlEventTouchUpInside];
        [contentView addSubview:button];
        [btns addObject:button];
        if (i == 1) {
            button.selected = YES;
        }
    }
    [btns mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.ma_offset(30);
        make.height.ma_offset(60);
    }];
    [btns mas_distributeRatioViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:40 leadSpacing:36 tailSpacing:36];
    _btns = btns;
    
    [contentView addSubview:self.arrowView];
    [_arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(btns[1]);
        make.top.ma_offset(92);
    }];
    
    UIView *fNameView = [UIView new];
    fNameView.layer.borderColor = MAColor(@"#FF0071").CGColor;
    fNameView.layer.borderWidth = 1;
    fNameView.layer.cornerRadius = MARatioWidth(9);
    [contentView addSubview:fNameView];
    [fNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(30);
        make.right.ma_offset(-30);
        make.height.ma_offset(48);
        make.top.ma_offset(142);
    }];
    _fNameView = fNameView;
    
    _fImgView = [UIImageView initWithName:DARE_F];
    [fNameView addSubview:_fImgView];
    [_fImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.ma_offset(4);
        make.bottom.ma_offset(-4);
    }];
    
    _fTextField = [UITextField new];
    _fTextField.font = MAFontScaleMedium(18);
    _fTextField.placeholder = @"女士名字";
    _fTextField.textAlignment = NSTextAlignmentCenter;
    [fNameView addSubview:_fTextField];
    [_fTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(52);
        make.right.ma_offset(-16);
        make.centerY.mas_equalTo(0);
    }];
    [_fTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    UIView *mNameView = [UIView new];
    mNameView.layer.borderColor = MAColor(@"#26A5FC").CGColor;
    mNameView.layer.borderWidth = 1;
    mNameView.layer.cornerRadius = MARatioWidth(9);
    [contentView addSubview:mNameView];
    [mNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(30);
        make.right.ma_offset(-30);
        make.height.ma_offset(48);
        make.top.ma_offset(220);
    }];
    _mNameView = mNameView;
    
    _mImgView = [UIImageView initWithName:DARE_M];
    [mNameView addSubview:_mImgView];
    [_mImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.ma_offset(4);
        make.bottom.ma_offset(-4);
    }];
    
    _mTextField = [UITextField new];
    _mTextField.placeholder = @"男士名字";
    _mTextField.font = MAFontScaleMedium(18);
    _mTextField.textAlignment = NSTextAlignmentCenter;
    [mNameView addSubview:_mTextField];
    [_mTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(52);
        make.right.ma_offset(-16);
        make.centerY.mas_equalTo(0);
    }];
    [_mTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    UIButton *beginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [beginBtn setBackgroundColor:MAColor(@"#FF0071")];
    beginBtn.layer.cornerRadius = MARatioWidth(24);
    beginBtn.layer.masksToBounds = YES;
    [beginBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [beginBtn setTitle:@"开始" forState:UIControlStateNormal];
    [beginBtn.titleLabel setFont:MAFontScaleMedium(16)];
    [contentView addSubview:beginBtn];
    [beginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.ma_offset(30);
        make.right.ma_offset(-30);
        make.bottom.ma_offset(-36);
        make.height.ma_offset(48);
    }];
    [beginBtn addTarget:self action:@selector(pushDareVC) forControlEvents:UIControlEventTouchUpInside];
}

- (void)textFieldDidChange:(UITextField *)textField{
    UITextRange *selectedRange = [textField markedTextRange];
    // 获取高亮部分,
    UITextPosition *pos = [textField positionFromPosition:selectedRange.start offset:0];
    if (selectedRange && pos) {//如果存在高亮部分, 就暂时不统计字数
        return;
    }
    NSInteger realLength = textField.text.length;
    if (realLength > 10) {
        textField.text = [textField.text substringToIndex:10];
    }
}

- (void)clickEventWithButton:(UIButton *)button {
    for (UIButton *btn in _btns) {
        btn.selected = NO;
    }
    button.selected = YES;
    [_arrowView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.ma_offset(92);
        make.centerX.mas_equalTo(button);
    }];
    [_arrowView layoutIfNeeded];
    
    _type = button.tag;
    if (button.tag == 0) {
        _fImgView.image = [UIImage imageNamed:DARE_M];
        _mImgView.image = [UIImage imageNamed:DARE_M];
        _fNameView.layer.borderColor = MAColor(@"#26A5FC").CGColor;
        _mNameView.layer.borderColor = MAColor(@"#26A5FC").CGColor;
        _fTextField.placeholder = @"男士名字";
        _mTextField.placeholder = @"男士名字";
    } else if (button.tag == 1) {
        _fImgView.image = [UIImage imageNamed:DARE_F];
        _mImgView.image = [UIImage imageNamed:DARE_M];
        _fNameView.layer.borderColor = MAColor(@"#FF0071").CGColor;
        _mNameView.layer.borderColor = MAColor(@"#26A5FC").CGColor;
        _fTextField.placeholder = @"女士名字";
        _mTextField.placeholder = @"男士名字";
    } else if (button.tag == 2) {
        _fImgView.image = [UIImage imageNamed:DARE_F];
        _mImgView.image = [UIImage imageNamed:DARE_F];
        _fNameView.layer.borderColor = MAColor(@"#FF0071").CGColor;
        _mNameView.layer.borderColor = MAColor(@"#FF0071").CGColor;
        _fTextField.placeholder = @"女士名字";
        _mTextField.placeholder = @"女士名字";
    }
}

- (void)pushDareVC {
    if (_mTextField.text.length > 0 && _fTextField.text.length > 0) {
        if ([_mTextField.text isEqualToString:_fTextField.text]) {
            [self.view makeToast:@"所给名字不可相同"];
        } else {
            MADareViewController *dareVC = [MADareViewController new];
            dareVC.model = self.model;
            dareVC.fName = _fTextField.text;
            dareVC.mName = _mTextField.text;
            dareVC.type = self.type;
            [self.navigationController pushViewController:dareVC animated:YES];
        }
    } else {
        [self.view makeToast:@"请输入名字"];
    }    
}


- (UIImageView *)arrowView {
    if (_arrowView) return _arrowView;
    _arrowView = [UIImageView initWithName:@"Dare_Arrow"];
    return _arrowView;
}

@end
